//
//  KEClassifyVC.swift
//  KoalaEn
//
//  Created by Wade on 2024/11/3.
//

import Foundation
import UIKit
import Alamofire
import SVProgressHUD
class KEClassifyVC : KYBaseVC ,UICollectionViewDelegate,UICollectionViewDataSource{
    var aBtn:UIButton?
    var selectIndex:Int = 0
    var gameView:KEGameView?
    var preBtn:UIButton?
    var nextBtn:UIButton?
    var contentView:UIView?
    
    var classifyList:Array<KEClassifyList>?
    var wordList:Array<KEWordsDetail>?
    override func viewDidLoad() {
        self.view.backgroundColor = .MainBGColor
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        setupSubViews()
        self.audioPlayer = ZFAudioPlayer()
        self.loadData()
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.classifyList?.count ?? 0;
    }
    
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.classifyList![section]._id?.aKoalaWordsCategories?.count ?? 0
    
    }
    
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "KEClassifyListViewCell", for: indexPath) as! KEClassifyListViewCell
        cell.categoryDetail = self.classifyList?[indexPath.section]._id?.aKoalaWordsCategories?[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("didselct")
        
        var classifyIds = Array<String>()
        for item in self.classifyList![indexPath.section]._id!.aKoalaWordsCategories! {
            if item._id != self.classifyList?[indexPath.section]._id?.aKoalaWordsCategories?[indexPath.item]._id {
                classifyIds.append(item._id ?? "")
            }
        }
        
        self.pushToDetailVC(categoryId: self.classifyList?[indexPath.section]._id?.aKoalaWordsCategories?[indexPath.item]._id,classifyIds: classifyIds)
    }
 
    
    // 获取
    func pushToDetailVC(categoryId:String?,classifyIds:Array<String>) {
            SVProgressHUD.show(withStatus: "正在加载...")
            let param = ["word_category_id": categoryId ?? "", "level": 2] as [String: Any]
            AF.request("https://fc-mp-d6f030db-0e27-458b-906b-ae7d21ab9004.next.bspapp.com/getWordClassify/getCategoryWordList", method: .post, parameters: param, encoding: JSONEncoding.default).responseDecodable(of: KEWordResult.self) { res in
                DispatchQueue.main.async {
                    SVProgressHUD.dismiss()
                }
                switch res.result {
                case .success(let result):
                    let words = result.result
                    if words!.count > 0 {
                        self.wordList = words;
                        DispatchQueue.main.async {
                           let detailVC = KEClassifyListDetailVC()
                            detailVC.menuList = self.wordList
                            detailVC.category_ids = classifyIds
                            self.navigationController?.pushViewController(detailVC, animated: true)
                        }
                    }
                case .failure:
                    print("error")
                    DispatchQueue.main.async {
                        SVProgressHUD.dismiss()
                    }
                }
            }
        
        
    }
    
    // 获取
    func loadData() {
        SVProgressHUD.show(withStatus: "正在加载...")
        AF.request("https://fc-mp-d6f030db-0e27-458b-906b-ae7d21ab9004.next.bspapp.com/getWordClassify/getWordClassify2", method: .post, parameters: nil, encoding: JSONEncoding.default).responseDecodable(of: KEClassifyListResult.self) { res in
            DispatchQueue.main.async {
                SVProgressHUD.dismiss()
            }
            switch res.result {
            case .success(let result):
                let words = result.result
                if words!.count > 0 {
                    self.classifyList = words;
                    DispatchQueue.main.async {
                        self.collectionView?.reloadData()
                    }
                }
            case .failure:
                print("error")
                DispatchQueue.main.async {
                    SVProgressHUD.dismiss()
                }
            }
        }
    }
    
  
}
